﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using AspNetCs.Common;
using AspNetCs.Enumerations.Channel_Enum;
using AspNetCs.Enumerations;

public partial class SearchList : BasePage
{
    private int pageSize = 20;
    protected int recodCount = 0;

    /// <summary>
    /// 频道
    /// </summary>
    protected int channelType = 0;
    /// <summary>
    /// 按编号还是名称查
    /// </summary>
    protected int isNumer = 0;
    /// <summary>
    /// 编号或名称的值
    /// </summary>
    protected string idOrName = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if ((!string.IsNullOrEmpty(Request["channelType"])) && (!string.IsNullOrEmpty(Request["isNumer"])) && (Request["idOrName"] != null))
            {
                channelType = StringHelper.isNumber_int(Request["channelType"]);
                isNumer = StringHelper.isNumber_int(Request["isNumer"]);
                idOrName = Request["idOrName"];
                ViewState["channelType"] = channelType;
                ViewState["isNumer"] = isNumer;
                ViewState["idOrName"] = idOrName;
            }
            else
            { 
                ResultStr(configInfo.InstallDir + "Users/Login.aspx","参数错误");

            }
            BindReapter();
        }
        else
        {
            if (ViewState["channelType"] != null)
            {
                channelType = StringHelper.isNumber_int(ViewState["channelType"].ToString());
            }
            if(ViewState["isNumer"] != null)
            {
                isNumer = StringHelper.isNumber_int(ViewState["isNumer"].ToString());
            }
            if (ViewState["idOrName"] != null)
            {
                idOrName = ViewState["isNumer"].ToString();
            }
        }

    }

    protected string MuTypeStr(object _is)
    {
        string str = "";
        try
        {

            NetMuTypeEnum NetMuType = (NetMuTypeEnum)StringHelper.isNumber_int(_is);
            str = EnumDescription.GetFieldText(NetMuType);

        }
        catch
        {
            str = "";
        }
        return str;


    }


    public void BindReapter()
    {
        SetVisiableOrDisplay(false);
        switch(channelType)
        {
            case (int)Channel_Type.cemetery:
                palCemetery.Visible = true;
                DataBindCemetery();
                break;
            case (int)Channel_Type.clan:
                palAncestralHall.Visible = true;
                DataBindAncestralHall();
                break;
            case (int)Channel_Type.gravestone:
                palNetMu.Visible = true;
                DataBindNetMu();

                break;
            case (int)Channel_Type.memorial:
                palMemorial.Visible = true;
                DataBindMemorial();
                break;
        }
    }

    public void SetVisiableOrDisplay(bool bo)
    {
        palMemorial.Visible = bo;
        palAncestralHall.Visible = bo;
        palNetMu.Visible = bo;
        palCemetery.Visible = bo;
    }

    public void DataBindMemorial()
    {
        string cmdText = "select id,Numid,names,createname=(select username from users where userid=me.CreateId ),Addtime,hot from memorial as me where IsCheck=1 and Isdel=0 and IsWelfare=0 {0} order by hot desc";
        string strWhere = "";
        if (!string.IsNullOrEmpty(idOrName))
        {
            if (isNumer == 1)
            {
                strWhere = "and Numid like '%"+idOrName+"%'";
            }
            else
            {
                strWhere = "and names like '%" + idOrName + "%'";
            }
        }
        cmdText = string.Format(cmdText,strWhere);
        DataPagesOperate operate = new DataPagesOperate();
        AspNetPager1.PageSize = pageSize;
        operate.BindRepeater(cmdText, AspNetPager1, "hot desc", "id", rptList, out recodCount);
    }

    public void DataBindAncestralHall()
    {
        string cmdText = "select id,AncestralHallId,createname=(select username from users where userid=me.CreateId ),CreateTime,AcceeCount,Name from AncestralHall as me where IsCheck=1 and Isdel=0 {0} order by AcceeCount desc";
        string strWhere = "";
        if (!string.IsNullOrEmpty(idOrName))
        {
            if (isNumer == 1)
            {
                strWhere = "and AncestralHallId like '%" + idOrName + "%'";
            }
        }

        cmdText = string.Format(cmdText, strWhere);
        DataPagesOperate operate = new DataPagesOperate();
        AspNetPager1.PageSize = pageSize;
        operate.BindRepeater(cmdText, AspNetPager1, "AcceeCount desc", "id", rptAncestralHall, out recodCount);
    }

    public void DataBindNetMu()
    {
        string cmdText = "select id,MuId,MuName,createname=(select username from users where userid=me.CreateId ),CreateDate,hot,MuType from NetMu as me where IsCheck=1 and Isdel=0 {0} order by hot desc";
        string strWhere = "";
        if (!string.IsNullOrEmpty(idOrName))
        {
            if (isNumer == 1)
            {
                strWhere = "and MuId like '%" + idOrName + "%'";
            }
            else
            {
                strWhere = "and MuName like '%" + idOrName + "%'";
            }
        }

        cmdText = string.Format(cmdText, strWhere);
        DataPagesOperate operate = new DataPagesOperate();
        AspNetPager1.PageSize = pageSize;
        operate.BindRepeater(cmdText, AspNetPager1, "hot desc", "id", rptNetMu, out recodCount);

    }

    public void DataBindCemetery()
    {
        string cmdText = "select id,Numid,names,createname=(select username from users where userid=ce.CreateId ),Addtime,hot from Cemetery as ce where IsCheck=1 and Isdel=0 {0} order by hot desc";
        string strWhere = "";
        if (!string.IsNullOrEmpty(idOrName))
        {
            if (isNumer == 1)
            {
                strWhere = "and Numid like '%" + idOrName + "%'";
            }
            else
            {
                strWhere = "and names like '%" + idOrName + "%'";
            }
        }

        cmdText = string.Format(cmdText, strWhere);
        DataPagesOperate operate = new DataPagesOperate();
        AspNetPager1.PageSize = pageSize;
        operate.BindRepeater(cmdText, AspNetPager1, "hot desc", "id", rptCemetery, out recodCount);
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindReapter();
    }
    protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            if (rptList.Items.Count <= 0)
            {
                Panel pl = e.Item.FindControl("Pl_Nothing") as Panel;
                pl.Visible = true;
            }
        }
    }


    /// <summary>
    /// 宗祠
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rptAncestralHallList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            if (rptAncestralHall.Items.Count <= 0)
            {
                Panel pl = e.Item.FindControl("PlAncestralHallList_Nothing") as Panel;
                pl.Visible = true;
            }
        }
    }

    /// <summary>
    /// 公墓
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rptNetMu_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            if (rptNetMu.Items.Count <= 0)
            {
                Panel pl = e.Item.FindControl("PlNetMu_Nothing") as Panel;
                pl.Visible = true;
            }
        }
    }


    /// <summary>
    /// 陵园
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rptCemeteryList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            if (rptCemetery.Items.Count <= 0)
            {
                Panel pl = e.Item.FindControl("PlCemetery_Nothing") as Panel;
                pl.Visible = true;
            }
        }
    }
}
